# -*- coding: utf-8 -*-

from odoo import fields, api
from odoo_contrib import models as contrib_models


class Category(contrib_models.SchemaModel):
    _name = 'exhibition.category'
    _description = 'Category for exhibits'

    name = fields.Char(string='分类名称', required=True)
    desc = fields.Char(string='分类描述')
    image = fields.Binary(string='分类图片', attachment=True)
    enabled = fields.Boolean(string='是否启用', default=True)

    image_id = fields.Integer(compute='_compute_image_id')

    @api.one
    @api.depends('image')
    def _compute_image_id(self):
        attachment = self.env['ir.attachment'].search([
            ('res_model', '=', 'exhibition.category'),
            ('res_field', '=', 'image'),
            ('res_id', '=', self.id)
        ], limit=1)
        self.image_id = attachment.id

    @api.multi
    def unlink(self):
        for category in self:
            category.enabled = False
        return True

    @api.multi
    def toggle_enabled(self):
        for category in self:
            category.enabled = not category.enabled
